package com.easy.controller;

import com.easy.bean.Order;
import com.easy.common.CommonResult;
import com.easy.service.IOrderService;
import com.easy.util.LogisticsOrderStatistics;
import com.easy.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class OrderController {

    @Autowired
    IOrderService orderService;

    //通过ID获取订单
    @GetMapping("order/{id}")
    public Order getOrderByID(@PathVariable int id) {
        return orderService.getOrderByID(id);
    }

    //获取订单列表
    @GetMapping("order")
    @Transactional
    public CommonResult getOrder(Order order, Page page) {
        List<Order> list =orderService.getOrder(order,page);
        int count=orderService.getCount(order);
        HashMap map=new HashMap();
        map.put("list",list);
        map.put("count",count);
        return CommonResult.success(map);
    }

    //删除订单
    @DeleteMapping("order/{id}")
    public CommonResult delOrder(@PathVariable int id) {
        int count = orderService.delOrder(id);
        if(count>0){
            return CommonResult.success(count);
        }else {
            return CommonResult.fail();
        }

    }


    //新增订单JSON
    @PostMapping("orderjson")
    @Transactional
    public CommonResult addOrderJSON(@RequestBody Order order){
        orderService.addOrder(order);
        order = orderService.getOrderByID(order.getId());
        return CommonResult.success(order);
    }

    //修改订单
    @PutMapping("orderjson")
    @Transactional
    public CommonResult editOrderJSON(@RequestBody Order order) {
        orderService.editOrder(order);
        order = orderService.getOrderByID(order.getId());
        return CommonResult.success(order);
    }

    @PutMapping("orderstatus")
    @Transactional
    public CommonResult editOrderStatus(@RequestBody Order order) {
        order = orderService.editOrderStatus(order);
        return CommonResult.success(order);
    }

    @GetMapping("chartdata")
    @Transactional
    public CommonResult getCount(){
        LogisticsOrderStatistics statistics = orderService.getStatistics();
        return CommonResult.success(statistics);
    }

    @GetMapping("orderbystation")
    public CommonResult getOrderByStationCode(Order order) {
        List<Order> list =orderService.getOrderByStationCode(order);
        return CommonResult.success(list);
    }
}
